/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PurchaseOrder;

import StoreType.StoreTypeBean;
import User.UserBean;
import Vendor.VendorBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author aamod
 */
public class PurchaseOrderList extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Vector<Object> list = PurchaseOrder.Query.viewAll();
            if (!list.isEmpty()) {
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    PurchaseOrderBean pob = (PurchaseOrderBean) it.next();
                    VendorBean vb = pob.getVendorBean();
                    StoreTypeBean sb = pob.getStoreTypeBean();
                    UserBean ub = pob.getUserBean();
                    String approvalstatus = "<span class='label label-success'>In Process</span>";
                    if (pob.getApprovalStatus() == 1) {
                        approvalstatus = "<span class='label label-success'>Approved</span>";
                    } else if (pob.getApprovalStatus() == 2) {
                        approvalstatus = "<span class='label label-error'>Rejected</span>";
                    }
                    out.println("<tr id='" + pob.getOrderId() + "'>");
                    out.println("<td><center>");
                    out.println(pob.getPurchaseOrderNo());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(pob.getProductName());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(vb.getVendorName());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(pob.getQty());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(pob.getUnitPrice());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(pob.getTax());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(pob.gettAmount());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(pob.getPurchaseOrderDate());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(approvalstatus);
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(ub.getEmployeeName());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println(pob.getDescription());
                    out.println("</center></td>");
                    out.println("<td><center>");
                    out.println("<a href='EditPurchaseOrder.jsp?id=" + pob.getOrderId() + "&vendorId=" + vb.getVendorId() + "&vendorname=" + vb.getVendorName() + "&storeTypeId=" + sb.getStoreTypeId() + "&storeType=" + sb.getStoreType() + "&pname=" + pob.getProductName() + "&qty="
                            + pob.getQty() + "&amount=" + pob.getUnitPrice() + "&tax=" + pob.getTax() + "&tamount=" + pob.gettAmount() + "&approvalStatus=" + pob.getApprovalStatus()
                            + "&approvedBy=" + pob.getApprovedBy() + "&pod=" + pob.getPurchaseOrderDate() + "&podNo=" + pob.getPurchaseOrderNo() + "&approvedDate=" + pob.getApprovedDate()
                            + "&approvalNo=" + pob.getApprovalNo() + "&desc=" + pob.getDescription() + "'><button class='btn btn-info'>Edit</button></a>");
                    out.println("<a href='#'><button class='btn btn-danger' onclick='deleteOrderConfirm(" + pob.getOrderId() + ")'>Delete</button></a>");
                    out.println("</center></td>");
                    out.println("</tr>");
                }
            } else {
                out.println("<tr>");
                out.println("<td colspan='14'>");
                out.println("<b>No Record Found</b>");
                out.println("</td>");
                out.println("</tr>");
            }
        } catch (Exception e) {

            out.println("<tr>");
            out.println("<td colspan='13'>");
            out.println("<b>No Record Found " + e + "+</b>");
            out.println("</td>");
            out.println("</tr>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(PurchaseOrderList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(PurchaseOrderList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
